<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<title>db-config-add</title>
		<%@ include file="../include/common.jsp"%>
		<%@ include file="../include/form.jsp"%>
		
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/jquery-ui.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/select/jquery.multiselect.min.js"></script>
	<link rel="stylesheet"  type="text/css" href="${pageContext.request.contextPath}/style/default/select/jquery.multiselect.css" />
	<link rel="stylesheet"  type="text/css" href="${pageContext.request.contextPath}/style/default/jqueryui/jquery-ui.css" />
	
		<script type="text/javascript">
  
  function add(){
	  //校验(ip+port+instName)是否唯一
	  
	  $.ajax({
		   type: "POST", 
		   url: "${pageContext.request.contextPath}/config/db-config!valideIpPortInstName.action", //请求的action
		   data: "ip="+$("#ip").val()+"&port="+$("#port").val()+"&instName="+$("#instName").val()+"&isValideFlag="+"true", //传的参数
		   dataType: 'json',
		   success: function(data){ //结果
			   if(data == true){
				   //提交
					 $.ajax({
						   type: "POST", 
						   url: "${pageContext.request.contextPath}/config/db-config!save.action", //请求的action
						   data: $("#myform").serialize(), //传的参数
						   dataType: 'json',
						   success: function(data){ //结果
							   if(data == true){
							   	   var isRef = '${isRef}';
							   	   if(isRef=='null'||isRef==''){
									   var pWin = getTopWin(window).currentWin["add-db-config"];
									   pWin.datagrid();
								   }
								   getTopWin(window).closejBox("db-host-config");
							   }
							   
						   }
					});
			   }else{
				   getTopWin(window).getMessageBox().alert("ip地址+实例名+端口号必须唯一存在","提示");
			   }
		   }
	});
  }
  
  //添加负责人的代码
  /**
  $(function(){
	  $.ajax({
			url : '${pageContext.request.contextPath}/config/host-config!getAllUsers.action', // 
			type : 'POST',
			dataType : "json",
			data : {},
			success : function(data) {
				$.each(data.users, function(i, index) {
					$('#administrator').append('<option  value='+index.userId+'>' + index.userName+ '</option> ');
				});
				
				$('#administrator').multiselect({
					   selectedList: 2
				});
			}
		});
  });*/
  
  $(function(){
	 $("#url").html("jdbc:oracle:thin:@$ip:$port:$dbname");
	  $("#instType").change(function(){
		  if($("#instType").val() == 'oracle'){
			  $("#url").html("jdbc:oracle:thin:@$ip:$port:$dbname");
		  }
		  if($("#instType").val() == 'db2'){
			  $("#url").html("jdbc:db2://$ip:$port/$dbname");
		  }
		  if($("#instType").val() == 'sybase'){
			  $("#url").html("jdbc:sybase:Tds:$ip:$port");
		  }
		  if($("#instType").val() == 'sqlserver'){
			  $("#url").html("jdbc:sqlserver://$ip:$port");
		  }
		  if($("#instType").val() == 'mysql'){
			  $("#url").html("jdbc:mysql://$ip:$port/$dbname");
		  }
		  if($("#instType").val() == 'informix'){
			  $("#url").html("jdbc:informix-sqli://$ip:$port/sysmaster:INFORMIXSERVER=$dbname");
		  }
	  });
	  
	  $.formValidator.initConfig({formid:"myform"
          ,onerror:function(msg){}
          ,onsuccess:function(){
        	  		add();
          			return false;
          }
          });
	  
	  $("#ip").formValidator({onshow:"请输入正确格式的IP",onfocus:"请输入正确格式的IP",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"})
	  .functionValidator({
			    fun:function(val,elem){
			    	var reg = /^([0-9]|[1-9][0-9]|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9][0-9]|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9][0-9]|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9][0-9]|1\d\d|2[0-4]\d|25[0-5])$/;
			    	if(val.match(reg) == null){
			    		return "格式不正确"
			        }
			        return true;
				}
	  });
	  $("#port").formValidator({onshow:"请输入数字 长度范围(1-5)",onfocus:"请输入数字 长度范围(1-5)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:5,onerror:"长度超出最大范围"})
	  .functionValidator({
			    fun:function(val,elem){
			    	 var reg = /^[0-9]+$/;
			    	    if(!reg.test(val)){
			    	        return "必须为数字";
			    	    }
			    	    return true;
				}
	  });
	  $("#userName").formValidator({onshow:"请输入字符串 长度范围(1-32)",onfocus:"请输入字符串  长度范围(1-32)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({empty:{leftempty:false,rightempty:false,emptyerror:"两边不能有空格"},onerror:"不能为空"})
	  .functionValidator({
			    fun:function(val,elem){
			    	    if(val.replace(/[^\x00-\xff]/g, "xx").length > 32){
			    	        return "长度超出最大范围";
			    	    }
			    	    return true;
				}
	  });
	  $("#maxConn").formValidator({onshow:"请输入数字  长度范围(1-5)",onfocus:"请输入数字 长度范围(1-5)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:5,onerror:"长度超出最大范围"});
	  $("#pwd").formValidator({onshow:"请输入字符串  长度范围(1-32)",onfocus:"请输入字符串  长度范围(1-32)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({empty:{leftempty:false,rightempty:false,emptyerror:"两边不能有空格"},onerror:"不能为空"})
	  .functionValidator({
			    fun:function(val,elem){
			    	    if(val.replace(/[^\x00-\xff]/g, "xx").length > 32){
			    	        return "长度超出最大范围";
			    	    }
			    	    return true;
				}
	  });
	  $("#pwd1").formValidator({onshow:"2次密码要一致",onfocus:"2次密码要一致",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({empty:{leftempty:false,rightempty:false,emptyerror:"两边不能有空格"},onerror:"不能为空"}).compareValidator({desid:"pwd",operateor:"=",onerror:"2次密码不一致"});
	  $("#instName").formValidator({onshow:"请输入字符串 长度范围(1-32)",onfocus:"请输入字符串 长度范围(1-32)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({empty:{leftempty:false,rightempty:false,emptyerror:"两边不能有空格"},onerror:"不能为空"})
	  .functionValidator({
			    fun:function(val,elem){
			    	    if(val.replace(/[^\x00-\xff]/g, "xx").length > 32){
			    	        return "长度超出最大范围";
			    	    }
			    	    return true;
				}
	  });
  
  	  //如果是从新增资源页面访问
  	  var isResAdd = '${data}';
  	  if(isResAdd!=''){
  	  	$('#instType option').each(function(index,ob){
  	  		if(ob.text=='${data.resType}'){
  	  			$(ob).attr('selected','selected');
  	  			if($("#instType").val() == 'oracle'){
					  $("#url").html("jdbc:oracle:thin:@$ip:$port:$dbname");
				  }
				  if($("#instType").val() == 'db2'){
					  $("#url").html("jdbc:db2://$ip:$port/$dbname");
				  }
				  if($("#instType").val() == 'sybase'){
					  $("#url").html("jdbc:sybase:Tds:$ip:$port");
				  }
				  if($("#instType").val() == 'sqlserver'){
					  $("#url").html("jdbc:sqlserver://$ip:$port");
				  }
				  if($("#instType").val() == 'mysql'){
					  $("#url").html("jdbc:mysql://$ip:$port/$dbname");
				  }
				  if($("#instType").val() == 'informix'){
					  $("#url").html("jdbc:informix-sqli://$ip:$port/sysmaster:INFORMIXSERVER=$dbname");
				  }
  	  			return false;
  	  		}
  	  	});
  	  	
  	  	  //如果是从新增资源页面访问
  	  	var isResAdd = '${data}';
  	  	if(isResAdd!=''){
  	  		var ip = $('#ip');
  	  		ip.val('${data.ip}');
  	  		ip.focus();
  	  		$('#instName').val('${data.dbName}');
  	  	}
  	  	
  	  }
  });
  function closewin(){
	  getTopWin(window).closejBox("db-host-config");
  }
  function pageReset(){
	  $("#myform").get()[0].reset(); 
	  $("#url").html("jdbc:oracle:thin:@$ip:$port:$dbname");
	}
  function test(){
	  $("#testTable").css({"display":""});
	  $.ajax({
		   type: "POST", 
		   url: "${pageContext.request.contextPath}/config/config-conn!allProbe.action", //请求的action
		   data: '', //传的参数
		   dataType: 'json',
		   success: function(data){ //结果
			   var obj= document.getElementById("result");
			   obj.length=0;
			   if(data.length == 0){
				   obj.options[0]=new Option("没有正在运行的probe!");
			   }else{
				   for(var i = 0; i < data.length; i++){
					   obj.options[i]=new Option(data[i].name+"   测试中，请等待...",data[i].id);
				   }
				   var num = 0;
				   connTest(data,num);
			   }
		   }
		});
  }
  
  function connTest(probe,num){
		  $.ajax({
				type : "POST",
				url : "${pageContext.request.contextPath}/config/config-conn!connTest.action", //请求的action
				data : $("#myform").serialize()+"&type="+'JDBC'+"&propId="+probe[num].id, //传的参数
				dataType : 'json',
				success : function(data) { //结果
					var obj= document.getElementById("result");
					if(data.flag == "true"){
						obj.options[num].text = "通过	"+probe[num].name+"	与	"+data.ip+"	连接成功！";
					}else{
						obj.options[num].text = "通过	"+probe[num].name+"	与	"+data.ip+"	连接失败！";
					}
					num++;
					if(num < probe.length){
						connTest(probe,num);
					}
				}
		});
  } 
  </script>
	</head>

	<body class="padding-8">
		<form action="" id="myform" name="myform" method="post">
			<table class="table_eidt">
				<tr>
					<td class="td_left" width="100px">
						<span class="span_alert">*</span>数据库类型
					</td>
					<td class="td_right">
						<select name="instType" id="instType" style="width: 225px">
							<option value="oracle">
								oracle
							</option>
							<option value="db2">
								db2
							</option>
							<option value="sybase">
								sybase
							</option>
							<option value="sqlserver">
								sqlserver
							</option>
							<option value="mysql">
								mysql
							</option>
							<option value="informix">
								informix
							</option>
						</select>
						<span id="instTypeTip" style="width: 150px"></span>
					</td>

				</tr>
				<tr>
					<td class="td_left">
						连接串
					</td>
					<td class="td_right">
						<div id="url"></div>
					</td>
				</tr>
				<tr>
					<td class="td_left">
						<span class="span_alert">*</span>数据库IP
					</td>
					<td class="td_right">
						<input type="text" name="ip" id="ip" class="input_text">
						<span id="ipTip" style="width: 190px"></span>
					</td>

				</tr>
				<!-- 
				<tr>
	    			<td class="td_left">负责人</td>
	    			<td class="td_right" colspan="2" >
		      	   		<select id="administrator" name="administrator" multiple="multiple" size="1"  class="select" style="width:425px;height:500px;text-align:center;" >
		      	   		</select>
	    			</td>
    			</tr>
    			 -->
				<tr style="height: 30px">
					<td class="td_left">
						<span class="span_alert">*</span>端口
					</td>
					<td class="td_right">
						<input type="text" name="port" id="port" class="input_text">
						<span id="portTip" style="width: 190px"></span>
					</td>
				</tr>
				<tr style="height: 30px">
					<td class="td_left">
						<span class="span_alert">*</span>数据库名
					</td>
					<td class="td_right">
						<input type="text" name="instName" id="instName"
							class="input_text">
						<span id="instNameTip" style="width: 190px"></span>
					</td>
				</tr>
				<tr style="height: 30px">
					<td class="td_left">
						<span class="span_alert">*</span>帐号
					</td>
					<td class="td_right">
						<input type="text" name="userName" id="userName"
							class="input_text">
						<span id="userNameTip" style="width: 190px"></span>
					</td>
				</tr>
				<tr style="height: 30px">
					<td class="td_left">
						<span class="span_alert">*</span>密码
					</td>
					<td class="td_right">
						<input type="password" name="pwd" id="pwd" class="input_text">
						<span id="pwdTip" style="width: 190px"></span>
					</td>
				</tr>
				<tr style="height: 30px">
					<td class="td_left">
						<span class="span_alert">*</span>确认密码
					</td>
					<td class="td_right">
						<input type="password" name="pwd1" id="pwd1" class="input_text">
						<span id="pwd1Tip" style="width: 190px"></span>
					</td>
				</tr>
				<tr style="height: 30px">
					<td class="td_left">
						<span class="span_alert">*</span>最大连接数
					</td>
					<td class="td_right">
						<input type="text" name="maxConn" id="maxConn" value="2"
							class="input_text">
						<span id="maxConnTip" style="width: 190px"></span>
					</td>
				</tr>
			</table>
			<div align="center" style="margin-top: 9px;">
				<input type="button" id="connTest" name="connTest" value="测试"
					class="btn-4" onclick="test();">
				<input type="submit" id="button" name="button" value="保存"
					class="btn-4">
				<input type="button" value="重置" class="btn-4" onclick="pageReset();">
				<input type="button" value="取消" onclick="closewin();" class="btn-4">
			</div>
			<table class="table_eidt" style="margin-top: 9px; display: none;"
				id="testTable">
				<tr>
					<td class="td_left" width="100px">
						测试结果
					</td>
					<td class="td_right">
						<select multiple="multiple" id="result"
							style="width: 390px; height: 40px; border-style: solid; border-width: 1px; border-color: #cccccc; overflow: auto;"></select>
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>
